package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
//import com.sun.java.swing.plaf.windows.TMSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时先别管事务
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IRegionService regionService;
    public Destination getByParentId(String parentId){
        Query query = new Query();
//        //先查询  然后修改
        query.addCriteria(Criteria.where("_id").is(parentId));

        Destination destination = template.findOne(query,Destination.class);
        return destination;
    }

//    public void update(Destination destination){
//        Query query = new Query();
//        //先查询  然后修改
//        query.addCriteria(Criteria.where("_id").is(destination.getInfo()));
//        //根据id查询
//        Update update = new Update();
//        //设置更新对象
//        update.set("ishot",hot);
//        template.updateMulti(query, update, Region.class);
//    }
    public Destination get(String id) {
        Optional<Destination> optional = destinationRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }


    public List<Destination> list() {
        return destinationRepository.findAll();
    }





    @Override
    public void saveOrUpdate(Destination destination) {
        if(StringUtils.hasLength(destination.getId())){
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(destination.getId()));
            DBHelper.update(template, query, destination, "name","sn","refIds","ishot","sequence","info");
        }else{
            template.save(destination);
        }


    }

    @Override
    public Object getDestByDestinationId(String rid) {
        return null;
    }

    @Override
    public void changeHotValue(String id, int hot) {

    }

    @Override
    public void delete(String id) {

    }

    //根>>中国>>广东>>广州:
    public List<Destination> getToasts(String parentId) {
        if(!StringUtils.hasLength(parentId)){//不是空
            return Collections.emptyList();
        }
        List<Destination> list = new ArrayList<>();
        //查询当前目的地
        createToast(list, parentId);
        Collections.reverse(list);  //取反
        return list;
    }



    //递归查询父目录
    private void createToast(List<Destination> list, String parentId) {
        if(!StringUtils.hasLength(parentId)){
            return;//这里代表直接访问的时候  不带参数
        }
        System.out.println(parentId);
        Destination destination = this.getByParentId(parentId);
        list.add(destination);
        if(StringUtils.hasLength(destination.getParentId())){
            createToast(list, destination.getParentId());
        }
    }
    public Page<Destination> query(DestinationQuery qo){
         Query query = new Query();

         if(StringUtils.hasLength(qo.getKeyword())){
             query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
         }
        if(StringUtils.hasLength(qo.getParentId())){
             query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        }else{
            query.addCriteria(Criteria.where("parentId").is(null));
        }
         Long total = template.count(query,Destination.class);
         if(total == 0){
             return Page.empty();
         }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(),
                Sort.Direction.ASC,"_id");
         query.with(pageable);
         List<Destination> destinations = template.find(query,Destination.class);
         return new PageImpl<>(destinations,pageable,total);
    }

    @Override
    public List<Destination>  getDestByRegionIdForApi(String regionId) {
        //首先当父为国内是
        List<Destination> list = null;
        if("-1".equals(regionId)){
            //中国所有的省份
            list = destinationRepository.findByParentName("中国");
        }else{
            //当父不是国内时
            Region region = regionService.get(regionId);
            List<String> refIds = region.getRefIds();
            list = destinationRepository.findByIdIn(refIds);
        }
        //查询每个国家下边的子  遍历完成

        for (Destination dest : list) {
            Pageable pageable  = PageRequest.of(0, 5);//这里使用分页的方式来控制只展示前几个
            List<Destination> children = destinationRepository.findByParentId(dest.getId(), pageable);
            dest.setChildren(children);
        }
        return list;
    }

    @Override
    public Destination getByName(String keyword) {
        return destinationRepository.findByName(keyword);
    }
}
